/*  
 * @(#) Constants.java Create on 2014-03-13 下午04:03:30   
 *   
 * Copyright 2014 by fibrlink.   
 */

package com.springdemo.module.systemmanager.service;

import com.springdemo.basic.common.Constants;
import com.springdemo.basic.global.service.BaseService;
import com.springdemo.module.systemmanager.dao.MenuDao;
import com.springdemo.module.systemmanager.entity.MenuIconsBean;
import com.springdemo.module.systemmanager.entity.MenuPage;
import com.springdemo.module.systemmanager.entity.myentity.MyMenu;
import com.springdemo.tools.ConfigTools;
import com.springdemo.tools.StringHelper;
import com.springdemo.tools.jackjson.JackJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qiaowan
 * @date 2014-03-13
 */
@Service
public class MenuService<T> extends BaseService<T> {

    @Autowired
    private MenuDao<T> menuDao;

    public MenuDao<T> getDao() {
        return menuDao;
    }


    /**
     * 递归获取左侧菜单
     *
     * @param userId
     * @return
     * @throws Exception
     * @Title: queryMenuListBySyn
     * @data:2014-5-7上午09:54:31
     * @author:qiaowan
     */
    public List<MyMenu> queryMenuListBySyn( String userId, String type ) throws Exception {
        MyMenu menu = new MyMenu();
        menu.setUser_id(userId);
        //左侧菜单不查询根节点
        if (StringHelper.toTrim(type).equals("0")) {
            menu.setId("0");
        } else if (StringHelper.toTrim(type).equals("1")) {
            menu.setId("-1");
        }
        List<MyMenu> allMenuList = new ArrayList<MyMenu>();
        List<MyMenu> list = new ArrayList<MyMenu>();

            list = menuDao.queryMenuListByAdmin(menu);

        if (list != null && list.size() > 0) {
            allMenuList = renderChildrenTree(list, userId);
        }
        return allMenuList;
    }

    /**
     * 递归获取菜单
     *
     * @param list
     * @return
     * @throws Exception
     * @Title: renderChildrenTree
     * @data:2014-4-17下午02:11:11
     * @author:qiaowan
     */
    private List<MyMenu> renderChildrenTree( List<MyMenu> list, String userId ) throws Exception {
        for (int i = 0; list != null && i < list.size(); i++) {
            MyMenu entity = list.get(i);
            MyMenu menu = new MyMenu();
            menu.setUser_id(userId);
            menu.setId(list.get(i).getId());
            List<MyMenu> children = menuDao.queryMenuListByAdmin(menu);

            entity.setChildren(children);
            renderChildrenTree(children, userId);
        }
        return list;
    }

    /**
     * 得到菜单列表（combotree）
     *
     * @return
     * @throws Exception
     * @Title: queryMenuListBySyn
     * @data:2014-5-7上午10:46:38
     * @author:qiaowan
     */
    public List<MyMenu> queryMenuListBySyn() throws Exception {
        List<MyMenu> allMenuList = new ArrayList<>();
        List<MyMenu>  list = menuDao.queryMenuListByAdmin(null);
        if (list != null && list.size() > 0) {
            allMenuList = renderChildrenTree(list);
        }
        return allMenuList;
    }

    /**
     * 递归循环子节点信息
     *
     * @return
     * @throws Exception
     * @Title: renderChildrenTree
     * @data:2014-4-3上午11:09:24
     * @author:qiaowan
     */
    private List<MyMenu> renderChildrenTree( List<MyMenu> list ) throws Exception {
        for (int i = 0; list != null && i < list.size(); i++) {
            MyMenu entity = list.get(i);
            List<MyMenu> childrens = menuDao.queryMenuListByAdmin(entity);
            entity.setChildren(childrens);
            renderChildrenTree(childrens);
        }
        return list;
    }


    /**
     * 判断同一级别下名称是否重复 url是否重复
     *
     * @return
     * @throws Exception
     * @Title: hasUrl
     * @data:2014-3-18上午10:15:19
     * @author:qiaowan        String flags = this.getFromRequestParameter("flag");

     */
    public int hasUrlOrName( MenuPage menuPage ) throws Exception {
        int count = menuDao.hasUrlOrName(menuPage);
        return count;
    }

    /**
     * 删除菜单时修改排序
     *
     * @throws Exception
     * @Title: updateSort1
     * @data:2014-3-13下午04:23:11
     * @author:qiaowan
     */
    @SuppressWarnings("unchecked")
    public int updateByDelete( MenuPage menuPage ) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("pid", menuPage.getPid());
        map.put("sortnum", menuPage.getSort());
        menuDao.updateSortByDelete(map);
        MenuPage entity = new MenuPage();
        entity.setId(menuPage.getId());
        entity.setDeltag("1");

        return menuDao.updateBySelective((T) entity);
    }



    /**
     * 根据id获取对象
     *
     * @return
     * @throws Exception
     * @Title: queryMById
     * @data:2014-3-13下午04:24:44
     * @author:qiaowan
     */
    public MyMenu queryMById( MenuPage menuPage ) throws Exception {
        MyMenu menu = (MyMenu) menuDao.queryMById(menuPage);
        return menu;
    }

    /**
     * 根据父节点id得到子节点集合个数
     *
     * @param menuPage
     * @return
     * @throws Exception
     * @Title: getChildList
     * @data:2014-3-18上午10:15:55
     * @author:qiaowan
     */
    public int getChildCount( MenuPage menuPage ) throws Exception {
        int count = 0;
        if (menuPage != null) {
            count = menuDao.getChildCount(menuPage.getPid());
        }
        return count;
    }

    /**
     * 异步获取菜单列表
     *
     * @param menuPage
     * @return
     * @throws Exception
     * @Title: getmenuList
     * @data:2014-3-18上午10:17:01
     * @author:qiaowan
     */
    public List<MyMenu> getmenuList( MenuPage menuPage ) throws Exception {
        return menuDao.getmenuList(menuPage);
    }

    /**
     * 获取所有的菜单信息
     *
     * @throws Exception
     * @Title: findAllModuleList
     * @data:2014-4-9下午06:48:58
     * @author:qiaowan
     */
    public List<MyMenu> findAllModuleList() throws Exception {
        List<MyMenu> parentList = new ArrayList<>();
        List<MyMenu> listmenu = menuDao.findAllModuleList();
        if (listmenu != null) {
            for (MyMenu module : listmenu) {
                if (!StringHelper.toTrim(module.getPid()).equals("") && module.getPid().equals("0")) {
                    parentList.add(module);
                    List<MyMenu> childList = new ArrayList<MyMenu>();
                    for (MyMenu child : listmenu) {
                        String sid = child.getPid();
                        if (sid != null && sid.equals(module.getId())) {
                            childList.add(child);
                        }
                    }
                    module.setChildren(childList);
                }
            }
        }
        return parentList;

    }

    ;

    /**
     * 获取整个左侧菜单
     *
     * @return
     * @throws Exception
     * @Title: findAllFunctionList
     * @data:2014-3-18上午10:14:17
     * @author:qiaowan
     */
    public List<MyMenu> findAllFunctionList( String id ) throws Exception {
        List<MyMenu> parentList = new ArrayList<MyMenu>();
        List<MyMenu> listmenu = null;
            listmenu = menuDao.findAllModuleList();

        if (listmenu != null) {
            for (MyMenu module : listmenu) {
                if (!StringHelper.toTrim(module.getPid()).equals("") && module.getPid().equals("0")) {
                    parentList.add(module);
                    List<MyMenu> childList = new ArrayList<MyMenu>();
                    for (MyMenu child : listmenu) {
                        String sid = child.getPid();
                        if (sid != null && sid.equals(module.getId())) {
                            childList.add(child);
                        }
                    }
                    module.setChildren(childList);
                }
            }
        }
        return parentList;

    }

    ;


    /**
     * 画菜单树
     *
     * @return
     * @throws Exception
     * @Title: getMenuHtml
     * @data:2014-4-29上午09:21:46
     * @author:san
     */
    public String getMenuHtml( List<MyMenu> menuList ) throws Exception {
        StringBuffer menuHtml = new StringBuffer();
        if (menuList != null && menuList.size() > 0) {
            for (int i = 1; i < menuList.size() + 1; i++) {
                MyMenu menu = menuList.get(i - 1);
                if (i % 2 != 0) {
                    menuHtml.append("<div class=\"accordion-group\">");
                    menuHtml.append("<div class=\"clearfix\">");
                    menuHtml.append("<a class=\"accordion-toggle\">");
                    menuHtml.append("<i class='" + menu.getIconCls() + "'>");
                    menuHtml.append("</i>");
                    menuHtml.append(menu.getText());
                    menuHtml.append("</a>");
                    if (i < menuList.size()) {
                        menuHtml.append("<div class=\"a-divider\"></div>");

                    }
                    if (i == menuList.size()) {
                        menuHtml.append("</div>");
                    }
                }
                if (i % 2 == 0) {
                    menuHtml.append("<a class=\"accordion-toggle \">");
                    menuHtml.append("<i class='" + menu.getIconCls() + "'></i>");
                    menuHtml.append(menu.getText());
                    menuHtml.append("</a>");
                    menuHtml.append("</div>");
                }
                if (i % 2 == 0 || i == menuList.size()) {
                    if (i % 2 == 0) {
                        MyMenu menu1 = menuList.get(i - 2);
                        if (menu1 != null && menu1.getChildren() != null && menu1.getChildren().size() > 0) {
                            menuHtml.append("<div id=\"" + menu1.getId() + "\" class=\"accordion-body collapse\">");
                            menuHtml.append(this.drawChilMenu(menu1));
                            menuHtml.append("</div>");
                        }
                        MyMenu menu2 = menuList.get(i - 1);
                        if (menu2 != null && menu2.getChildren() != null && menu2.getChildren().size() > 0) {
                            menuHtml.append("<div id=\"" + menu2.getId() + "\" class=\"accordion-body collapse\">");
                            menuHtml.append(this.drawChilMenu(menu2));
                            menuHtml.append("</div>");
                        }
                        menuHtml.append("</div>");

                    }
                    if (i == menuList.size() && i % 2 != 0) {
                        MyMenu menu1 = menuList.get(i - 1);
                        if (menu1 != null && menu1.getChildren() != null && menu1.getChildren().size() > 0) {
                            menuHtml.append("<div id=\"" + menu1.getId() + "\" class=\"accordion-body collapse\">");
                            menuHtml.append(this.drawChilMenu(menu1));
                            menuHtml.append("</div>");
                        }
                        menuHtml.append("</div>");
                    }

                }

            }

        }
        return menuHtml.toString();
    }

    /**
     * 画出子菜单
     *
     * @param menu
     * @return
     * @Title: drawChilMenu
     * @data:2014-5-4下午03:15:56
     * @author:san
     */
    public String drawChilMenu( MyMenu menu ) {
        StringBuffer menuHtml = new StringBuffer();
        List<MyMenu> childrenList = menu.getChildren();
        if (childrenList != null && childrenList.size() > 0) {
            menuHtml.append("<ul class=\"easyui-tree menu-tree\">");
            for (MyMenu myMenuC : childrenList) {
                menuHtml.append("<li>");
                //是菜单
                if (StringHelper.toTrim(myMenuC.getType()).equals("0")) {
                    menuHtml.append("<span>");
                    menuHtml.append(myMenuC.getText());
                    //查看是否还有子类
                    List<MyMenu> childrenList3 = myMenuC.getChildren();
                    if (childrenList3 != null && childrenList3.size() > 0) {
                        menuHtml.append("</span>");
                        menuHtml.append(drawChilMenu(myMenuC));
                    }
                }
                //页面
                else if (StringHelper.toTrim(myMenuC.getType()).equals("1")) {
                    String iconCls = "";
                    if (StringHelper.toTrim(myMenuC.getIconCls()).equals("")) {
                        iconCls = Constants.PAGE_DEFAULT_CLASS;
                    } else {
                        iconCls = myMenuC.getIconCls();
                    }
                    String node = myMenuC.getText() + "||" + iconCls + "||" + myMenuC.getUrl();
                    menuHtml.append("<span>");
                    menuHtml.append("<a href=\"javascript:addTab('" + node + "')\">");
                    menuHtml.append(myMenuC.getText());
                    menuHtml.append("</a>");
                }
                menuHtml.append("</span>");
                menuHtml.append("</li>");
            }
            menuHtml.append("</ul>");

        }
        return menuHtml.toString();

    }



}
